import { getPerson,writePerson,updatePerson } from './service';
import { message } from 'antd';
import moment from 'moment'
const creator=(type,payload)=>({type,payload})
export default {
  namespace: 'person',
  state: {
    previewData:{},
    allPerson:[],
    singlePerson:[{
      name:'',
      overview:[],
      chineseName:'',
      birthDate:'',
      englishName:'',
      deathDate:'',
      alias:'',
      college:'',
      nationality:'',
      career:'',
      nation:'',
      represent:[],
      birthPlace:'',
      achievement:[],
      lifetime:[],
      evaluation:'',
      imageUrl:'',
    }],
  },
  effects: {
    //获取人物列表
    *getPerson({payload},{call,put}) {
      const res=yield call(getPerson,payload)
      if(res.httpStatus===200){
        if(payload){//有值表获取单个用户
          yield put(creator('saveSinglePerson',res.data))
        }else{
          yield put(creator('savePerson',res.data))
        }
        
      }
      return res
    },
    *writePerson({payload},{call,put}){
      const res=yield call(writePerson,payload)
      return res;
     
    },
    *updatePerson({payload},{call,put}){
      const res=yield call(updatePerson,payload)
      return res;
    }
  },
  reducers: {
    savePreview(state,{payload}){
        return {
            ...state,
            previewData:{...payload}
        }
    },
    savePerson(state,{payload}){
      return{
        ...state,
        allPerson:payload
      }
    },
    saveSinglePerson(state,{payload}){
      return{
        ...state,
        singlePerson:payload
      }
    },
  },
};
